package grails4j
import twitter4j.Status
import twitter4j.Twitter
import twitter4j.TwitterException
import twitter4j.TwitterFactory
import twitter4j.http.AccessToken
import twitter4j.http.RequestToken

class LoginController {

    def index = { 
		redirect(action: "login", params: params)
	}
	
	def login = {
		if(session.autenticado == null){
			Twitter twitter = new TwitterFactory().getInstance()
			twitter.setOAuthConsumer("94l4la4Ews1sFTDwWxCTKQ", "jfM506MvlpzYEqalaQmUjdsFOECk6E3OfaGDVTgAw")
			RequestToken requestToken = twitter.getOAuthRequestToken("http://localhost:8080/grails4J/login/validando")
			
			flash.pagina = requestToken.getAuthorizationURL()
			
			//session.twitter = twitter
			
			session.requestToken = requestToken
			
		
		}
		else{
			redirect(controller:'principal',action:'index')
		}	
	}
	
	def validando = {
	
		String pin = params.oauth_verifier
		Twitter twitter = new TwitterFactory().getInstance()
		twitter.setOAuthConsumer("94l4la4Ews1sFTDwWxCTKQ", "jfM506MvlpzYEqalaQmUjdsFOECk6E3OfaGDVTgAw")
		//Twitter twitter = session.twitter
		
		RequestToken requestToken = session.requestToken
		
		AccessToken accessToken = null;
		
		accessToken = twitter.getOAuthAccessToken(requestToken, pin)
		
		session.twitter = twitter
		
		session.autenticado = accessToken
		
		redirect(controller:'principal',action:'index')
		
	}
	
}
